Qt.include("./Util/Util.js")
Qt.include("./Util/Database.js")
Qt.include("./WebAccess/TwitterAPI.js")

WorkerScript.onMessage = function(message) {
    outputDebugString("start worker script(search)");


    var date_string = "";
    var url = "";

    // DB登録
    appendItem(message.dest_model, message.src_obj
               , message.user_id
               , message.user_name);

    // ウエイト
    delay(1000);

    // メインスレッドへの応答
    WorkerScript.sendMessage({ "result": true, "split": 0, "item": message.src_obj })
}



function appendItem(dest_model, src_obj, user_id){


    if(!checkAuthInfo()){
        dest_model.append({"_message": "oAuth information is nothing."});
        dest_model.sync();
    }else if(src_obj.length === 0){
        dest_model.append({"_message": "object list is null."});
        dest_model.sync();
    }else{
        var today_string = Qt.formatDateTime(new Date(), "yyyy-MM-dd");
        var tweet_string = "";
        var title = "";
        var info = "";
        var url = "";
        var count = 0;
        var hashtag = "#sakidoritter";

        for(var i=0; i<src_obj.length; i++){
            var item = src_obj[i];

            if(today_string > item["_release_date"]){
                // 古い
//                outputDebugString("exist release:" + item["_asin"]);

//                outputLog(dest_model, i + ": on sale :" + user_id
//                       + ":ASIN=" + item["_asin"]
//                       + ",Title=" + item["_title"]
//                       + ",release=" + item["_release_date"]);

            }else if(!saveHistory(user_id, item["_asin"])){
                // 保存出来なかったので既出
//                outputLog(dest_model, i + ": exist :" + user_id
//                       + ":ASIN=" + item["_asin"]
//                       + ",Title=" + item["_title"]
//                       + ",release=" + item["_release_date"]);
            }else{
                // 保存できたので新規発見
                outputLog(dest_model, i + " : NEW : " + user_id + ":ASIN=" + item["_asin"]
                       + ",Title=" + item["_title"]
                       + ",release=" + item["_release_date"]);

                // つぶやき
                if(count > 0){
                }else{
                    title = item["_product_group"] + ":" + item["_title"];
                    info = "(発売日:" + item["_release_date"] + ")"
                    url = item["_detail_url"];
                }
                count++;
            }
        }
        // 登録しなかった数
        outputLog(dest_model, "on sale / exist : " + user_id + " : " + (src_obj.length - count));

        // ついーと
        if(count > 0){
            var cut_len = 80;
            if(count > 1){
                info += " ほか" + (count -1) + "点";
            }
            tweet_string = "@" + user_id + " " + title;
            cut_len = info.length + hashtag.length + 22 + 2;
            outputDebugString("tweet len=" + tweet_string.length + "," + cut_len);
            if(tweet_string.length > (140 - cut_len)){
                tweet_string = tweet_string.substring(0, 140 - cut_len - 3) + "...";
            }
            tweet_string += " " + info + " " + url + " " + hashtag;
            sendTweet(tweet_string, "", reponseTweet);
            outputDebugString("send tweet -> " + tweet_string);

            // ログ保存
            outputLog(dest_model, "send tweet -> " + user_id + " / " + title);
        }
    }
}
// 送信確認
function reponseTweet(obj){
    outputDebugString("tweeted");
}

